<?php

/**
 *
 * PHP API 发货发送消息控制器
 * @author zqy
 * powered by zqy 2011-06-22
 */
class SendMessageApp extends ApiApp
{
    function __construct()
    {
        $this->Send_messageApp();
    }

    function Send_messageApp()
    {
        parent::__construct();
    }

    /**
     * 验证是否有权限发送
     */
    protected function verifyIp()
    {
        $allow_send_message_ip = explode('|', ALLOW_SEND_MESSAGE_IP);

        $client_ip = real_ip();

        $is_send = false; //验证是否为允许发送IP,默认不允许发送

        if (is_array($allow_send_message_ip) && in_array($client_ip, $allow_send_message_ip)) {
            $is_send = true;
        } else {
            if ($client_ip == $allow_send_message_ip) {
                $is_send = true;
            }
        }
        return $is_send;
    }

    public function shippedNotify()
    {

        $is_send = $this->verifyIp();

        if ($is_send == false) {
            $return_data = array(
                'status' => 0,
                'code' => '0001',
            );
            return $this->return_data($return_data);
        }

        $order_id = (int)$_REQUEST['order_id'];
        $invoice_no = trim($_REQUEST['invoice_no']);
        $order_amount = (float)$_REQUEST['order_amount'];
        $order_model = & m('order');
        $member_model = & m('member');
        $order_info = $order_model->get($order_id);

        if (empty($order_info)) {
            $return_data = array(
                'status' => 0,
                'code' => '0003',
            );
            return $this->return_data($return_data);
        }

        $buyer_id = $order_info['buyer_id'];
        $buyer_info = $member_model->get($buyer_id);

        if ($order_info['order_amount'] != $order_amount) {
            $return_data = array(
                'status' => 0,
                'code' => '0002',
            );
            return $this->return_data($return_data);
        }

        list($mobile, $content) = $this->_get_mobilePM_info($order_info);
        $this->send_mobile_pm($mobile, $content);

        $order_info['invoice_no'] = $invoice_no;
        $mail = get_mail('tobuyer_shipped_notify', array('order' => $order_info));
        $mail_queue_id = $this->_mailto(
            $buyer_info['email'],
            addslashes($mail['subject']),
            addslashes($mail['message'])
        );

        if ($mail_queue_id) {
            $return_data = array(
                'status' => 1,
                'code' => '0000',
            );
            return $this->return_data($return_data);
        }
    }

    public function virtualRechargeNotify()
    {
        $is_send = $this->verifyIp();

        //非法ip地址
        if ($is_send == false) {
            $return_data = array(
                'status' => 0,
                'code' => '0001',
                'message' => '非法IP地址',
            );
            return $this->return_data($return_data);
        }

        $price = (float)$_REQUEST['price'];
        $user_id = (int)$_REQUEST['user_id'];

        if (empty($price)) {
            $return_data = array(
                'status' => 0,
                'code' => '0002',
                'message' => '参数有误'
            );

            return $this->return_data($return_data);
        }

        $member_model = & m('member');
        $buyer_info = $member_model->get($user_id);

        if (empty($buyer_info)) {
            $return_data = array(
                'status' => 0,
                'code' => '0003',
                'message' => '用户不存在',
            );

            return $this->return_data($return_data);
        }

        $message_content = array(
            'price' => $price
        );

        //礼品充值发送站内信
        $message_id = $this->sendMessage($user_id, 'mall_touser_virtual_account_recharge_notify', $message_content);

        if ($message_id) {
            $return_data = array(
                'status' => 1,
                'code' => '0000',
                'message' => '发送成功',
            );
        } else {
            $return_data = array(
                'status' => 0,
                'code' => '0004',
                'message' => '发送失败'
            );
        }
        return $this->return_data($return_data);
    }

    /**
     * 发送站内信
     */
    protected function sendMessage($user_id, $message_title, $message_content)
    {
        $ms =& ms();
        $content = get_msg($message_title, $message_content);
        return $msg_id = $ms->pm->send(MSG_SYSTEM, $user_id, '', $content);
    }


    private function return_data($data)
    {

        if (is_array($data)) {
            echo json_encode($data);
            return;
        }
    }

    /**
     * 根据$order_info 获得买家的手机号和短信内容
     * powered by zqy 2011-6-21
     * @param  $order_info
     */
    private function _get_mobilePM_info($order_info)
    {
        //获得买家的手机号码
        $order_extm_mod =& m('orderextm');

        $sql = sprintf(
            'SELECT phone_mob FROM %s WHERE order_id=%.0f',
            $order_extm_mod->table,
            (float)$order_info['order_id']
        );

        $mobile = $order_extm_mod->getCol($sql);
        if (empty($mobile[0])) {
            $member_mod =& m('member');
            $sql = sprintf(
                'SELECT phone_mob FROM %s WHERE user_id=%.0f',
                $member_mod->table,
                (float)$order_info['buyer_id']
            );
            $mobile = $member_mod->getCol($sql);
        }

        $mobile = $mobile[0];

        //获得短信内容
        $content = get_mobilePM('tobuyer_shipped_notify', array('order' => $order_info));
        return array($mobile, $content['message']);
    }

    /**
     * 发送手机短信
     * powered by zqy 2011-6-21
     */
    public function send_mobile_pm($mobile, $param)
    {
        if (empty($mobile) || empty($param)) {
            return;
        }
        $param = "sdst=" . trim($mobile) . "&smsg=" . $param;
        $lst = $this->post_request(SEND_MOBILE_PM_URL, $param);

        $rst = $lst['rst'];
        $info = $lst['info'];
        if ($info['http_code'] == 200) {
            return $rst;
        } else {
            return 0;
        }
    }


}